package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.CustomerTransferRecord;
import trlygjj.tanruiliyigenjinjin.domain.PotentialCustomer;
import trlygjj.tanruiliyigenjinjin.domain.ProfessionalStuent;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.PotentialCustomerQueryObject;
import trlygjj.tanruiliyigenjinjin.service.IPotentialCustomerService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.util.List;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("potentialCustomer")
public class PotentialCustomerController {

    @Autowired
    private IPotentialCustomerService potentialCustomerService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequestMapping("")
    public String index(){
        return "potentialCustomer/list";
    }
    @RequestMapping("inpool")
    public String inpool(){
        return "potentialCustomer/customerPoolLlist";
    }

    /**
     * 具有分页和高级查询
     * @return
     */
    @RequestMapping("list")
    @RequiresPermissions("potentialCustomer:list")
    @PermissionName("潜在客户列表页面")
    @ResponseBody
    public PageResult list(PotentialCustomerQueryObject qo){
        PageResult result = potentialCustomerService.query(qo);
        return result;
    }

    /** 查询资源池的客户
     * 具有分页和高级查询
     * @return
     */
    @RequestMapping("queryInpoolCustomer")
    @RequiresPermissions("potentialCustomer:queryInpoolCustomer")
    @PermissionName("资源池列表")
    @ResponseBody
    public PageResult queryInpoolCustomer(PotentialCustomerQueryObject qo){
        qo.setInpoolState(PotentialCustomerQueryObject.INPOOL_STATE);
        PageResult result = potentialCustomerService.query(qo);
        return result;
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("listAll")
    @ResponseBody
    public List<PotentialCustomer> listAll(){
        return potentialCustomerService.selectAll();
    }

    /**
     * 删除
     * @param potentialCustomerId 要删除的员工对象id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("delete")
    @RequiresPermissions("potentialCustomer:delete")
    @PermissionName("潜在客户删除")
    @ResponseBody
    public JsonResult delete(Long potentialCustomerId){
        JsonResult result = new JsonResult();
        try{
            potentialCustomerService.deleteByPrimaryKey(potentialCustomerId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     *更新
     * @param potentialCustomer 要更新的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("update")
    @RequiresPermissions("potentialCustomer:update")
    @PermissionName("潜在客户修改")
    @ResponseBody
    public JsonResult update(PotentialCustomer potentialCustomer){
        JsonResult result = new JsonResult();
        try{
            potentialCustomerService.updateByPrimaryKey(potentialCustomer);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 保存
     * @param potentialCustomer 要保存的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("save")
    @RequiresPermissions("potentialCustomer:save")
    @PermissionName("潜在客户新增")
    @ResponseBody
    public JsonResult save(PotentialCustomer potentialCustomer){
        JsonResult result = new JsonResult();
        try{
            potentialCustomerService.insert(potentialCustomer);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }
    /**
     * 放入资源池
     * @param  id 客户的id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("putlToPool")
    @RequiresPermissions("potentialCustomer:putlToPool")
    @PermissionName("潜在客户加入资源池")
    @ResponseBody
    public JsonResult putlToPool(Long id){
        JsonResult result = new JsonResult();
        try{
            potentialCustomerService.changeInpoolState(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }
    /**
     * 考试登记
     * @param  potentialCustomer 客户
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("tsetRegistration")
    @RequiresPermissions("potentialCustomer:tsetRegistration")
    @PermissionName("考试登记")
    @ResponseBody
    public JsonResult tsetRegistration(PotentialCustomer potentialCustomer){
        JsonResult result = new JsonResult();
        try{
            potentialCustomerService.tsetRegistration(potentialCustomer);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }
    /**
     * 指派给某人
     * @param  customerTransferRecord 移交记录表
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("assign")
    @RequiresPermissions("potentialCustomer:assign")
    @PermissionName("资源池客户指派")
    @ResponseBody
    public JsonResult assign(CustomerTransferRecord customerTransferRecord){
        JsonResult result = new JsonResult();
        try{
            potentialCustomerService.assign(customerTransferRecord);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 接收
     * @param  customerTransferRecord 移交记录表
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("receive")
    @RequiresPermissions("potentialCustomer:receive")
    @PermissionName("资源池客户接收")
    @ResponseBody
    public JsonResult receive(CustomerTransferRecord customerTransferRecord){
        JsonResult result = new JsonResult();
        try{
            potentialCustomerService.receive(customerTransferRecord);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }
    /**
     * 学员转正
     * @param  professionalStuent 正式学员表
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("positive")
    @RequiresPermissions("potentialCustomer:positive")
    @PermissionName("客户转正")
    @ResponseBody
    public JsonResult positive(ProfessionalStuent professionalStuent){
        JsonResult result = new JsonResult();
        try{
            potentialCustomerService.positive(professionalStuent);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }
    @RequestMapping("exportJxl")
    @RequiresPermissions("potentialCustomer:exportJxl")
    @PermissionName("客户导出")
    @ResponseBody
    public JsonResult exportJxl(){
        JsonResult result = new JsonResult();
        try{
            potentialCustomerService.exportJxl();
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }


    @RequestMapping("validateTell")
    @ResponseBody
    public Boolean validateTell(Long id,String tell) {
        PotentialCustomer potentialCustomer = potentialCustomerService.validateTell(tell);
        if (id == null) {
            return potentialCustomer == null;
        }
        return true;


    }


    @RequestMapping("validateQq")
    @ResponseBody
    public Boolean validateQq(Long id,String qq) {
        PotentialCustomer potentialCustomer = potentialCustomerService.validateQq(qq);
        if (id == null) {
            return potentialCustomer == null;
        }
        return true;


    }



}

